﻿using SourceMaster.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SourceMaster.Model
{
    public class MyBranch
    {
        // Canonical Name
        public string CanonicalName { get; set; }

        public string Name { get; set; }

        // Tracked remote branch, Tracked Branch means a remote branch
        public MyBranch TrackedBranch { get; set; }

        private string TrackedBranchName { get; set; }

        // A branch tip is the last commit or most recent commit on a branch
        public MyCommit Tip { get; set; }

        public string TipHash { get; set; }

        // whether this instance is a remote
        public bool IsRemote { get; set; }

        // if the local branch is connect to a remote one
        public bool IsTracking { get; set; }

        public LibGit2Sharp.Branch lbBranch { get; set; }

        public int RightMostVisualPosition { get; set; }

        public int AheadBy { get; set; }

        public int BehindBy { get; set; }

        private RepoViewModel repositoryViewModel { get; set; }

        public static MyBranch Create(RepoViewModel repositoryViewModel, LibGit2Sharp.Repository repo, LibGit2Sharp.Branch branch)
        {
            MyBranch myBranch = new MyBranch
            {
                CanonicalName = branch.CanonicalName,
                Name = branch.Name,
                IsRemote = branch.IsRemote,
                IsTracking = branch.IsTracking,
                TipHash = branch.Tip.Sha.ToString(),
                AheadBy = (int)branch.TrackingDetails.AheadBy,
                BehindBy = (int)branch.TrackingDetails.BehindBy,
                lbBranch = branch,
                TrackedBranchName = branch.TrackedBranch != null ? branch.TrackedBranch.Name : null
            };

            myBranch.repositoryViewModel = repositoryViewModel;

            // Loop through the first commitsPerpage number commits and let them now about this branch
            foreach (LibGit2Sharp.Commit branchCommit in branch.Commits.Take(repositoryViewModel.CommitsPerPage))
            {
                MyCommit myCommit = repositoryViewModel.MyCommits.Where(c => c.Hash == branchCommit.Sha.ToString()).FirstOrDefault();
                if (myCommit != null)
                {
                    myCommit.Branches.Add(myBranch);
                    if (myBranch.TipHash == myCommit.Hash)
                    {
                        myCommit.DisplayTags.Add(branch.Name);
                        myBranch.Tip = myCommit;
                    }
                }
            }

            return myBranch;
        }

        public void PostProcess(ObservableCollection<MyBranch> myBranches, ObservableCollection<MyCommit> myCommits)
        {
            TrackedBranch = myBranches.Where(b => b.Name == TrackedBranchName).FirstOrDefault();
            Tip = myCommits.Where(c => c.Hash == TipHash).FirstOrDefault();
        }

        public void Delete()
        {
            LibGit2Sharp.Repository repo = new LibGit2Sharp.Repository(repositoryViewModel.RepoFullPath);
            //repo.Branches.Remove()
            repo.Branches.Remove(lbBranch);
            repo.Dispose();
            repositoryViewModel.LoadEntireRepository();
        }
    }
}
